package com.yycwxt.modules.admin.packfee;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.PackFee;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api("包装费接口")
@RestController
@Validated
@RequestMapping("/pf")
public class PackFeeController  {

    private final PackFeeService pfs;

    public PackFeeController(@Autowired PackFeeService pfs) {
        this.pfs = pfs;
    }
    @GetMapping("/find")
    @ApiOperation("查询全部包装费信息")
    public ReturnResult selectAll(){
        return ReturnResult.buildSuccessResult(pfs.list());
    }

    @GetMapping("/cfind")
    @ApiOperation("条件查询包装费信息")
    public ReturnResult selectByInput(@ApiParam(name = "input", required = true, type = "String")   String input){
        List list = pfs.selectByInput(input);
        return ReturnResult.buildSuccessResult(list);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除一条包装费数据")
    public ReturnResult delete( @PathVariable @ApiParam(name = "id", required = true ,type = "int") Integer id){
        try {
            Integer ids = Integer.valueOf(id);
            if (pfs.removeById(ids)) {
                return ReturnResult.buildSuccessResult(true);
            } else {
                return ReturnResult.buildFailureResult("id可能不存在", false);
            }
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("id必须为数字", false);
        }
    }

    @ApiOperation("编辑包装费用信息")
    @PutMapping("/update")
    public ReturnResult updateid(@ApiParam(name = "packfee",required = true,type = "PackFee")@RequestBody  @Validated PackFee packfee){
        try {
            if (pfs.updateById(packfee)) {
                return ReturnResult.buildSuccessResult(true);
            } else {
                return ReturnResult.buildFailureResult("id可能不存在或者name重复", false);
            }
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("name 值可能重复", false);
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }

    @ApiOperation("添加包装费用信息")
    @PostMapping("/add")
    public ReturnResult add(@ApiParam(name = "packfee",required = true,type = "PackFee")@RequestBody   PackFee packfee){
        try {
            return ReturnResult.buildSuccessResult(pfs.add(packfee));
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("id/name 值重复");
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }


    @ApiOperation("查询一共有哪些包装类型")
    @GetMapping("/findtype")
    public ReturnResult findtype(){
        return ReturnResult.buildSuccessResult(pfs.findtype());
    }
//    @PostMapping("/upload")
//    @ApiOperation("Excel导入数据")
//    public ReturnResult excelToDatabase(@ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
//        try {
//            if (!file.isEmpty()) {
//                if (pfs.importFileToDB(true, file)) {
//                    return ReturnResult.buildSuccessResult("导入成功", null);
//                } else {
//                    return ReturnResult.buildFailureResult("导入失败", null);
//                }
//            } else {
//                return ReturnResult.buildFailureResult("文件为空", null);
//            }
//        }
//        catch (Exception e){
//            return ReturnResult.buildFailureResult("不支持此文件类型 或 文件内容格式不对", null);
//        }
//    }

}
